//////////////////////////////////////////////////////////////////////////////////////////////
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	File			:	"MenuFont.cpp"
//	Authors			:	Jake Castillo,
//	Project			:	Project Aetheryte
//	Date Modified	:	8/6/2013
//	Purpose			:	Bitmap Font class for the title of the game.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//////////////////////////////////////////////////////////////////////////////////////////////

#include "TitleFont.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../Game Essentials/Game.h"

#include <cassert>

TitleFont::TitleFont(void) : Font()
{
	this->LookupTable = nullptr;
	m_nImageID = -1;
}


TitleFont::~TitleFont(void)
{
}

/*virtual*/ void TitleFont::Initialize( const TCHAR* szFilepath, int nWidth,  
									  int nHeight,  int nCols, int nRows, bool OnlyUpper,  TCHAR cFirstChar, unsigned int lang )
{
	// Load the image
	m_nImageID = CSGD_TextureManager::GetInstance()->LoadTexture(szFilepath);

	// Validate
	assert( m_nImageID != -1 && "CBitmapFont::Draw - image was not loaded." );

	m_nCharWidth		= nWidth;
	m_nCharHeight		= nHeight;
	m_nNumCols			= nCols;
	m_nNumRows			= nRows;

	m_bOnlyUppercase	= OnlyUpper;
	m_cFirstChar		= cFirstChar;

	language = lang;

	CreateLookup();
}

void TitleFont::Terminate( void )		
{
	CSGD_TextureManager::GetInstance()->UnloadTexture( m_nImageID );
	m_nImageID = -1;

	for(int i = 0; i < m_nNumRows; i++)
	{
		delete[] LookupTable[i];
	}

	delete[] LookupTable;
}

//void MenuFont::Draw( const TCHAR* strOut, int nX, int nY, float fScale, DWORD dwColor ) const			
//{
//	// Validate
//	assert( m_nImageID != -1 && "CBitmapFont::Draw - image was not loaded." );
//	assert( strOut != nullptr && "CBitmapFont::Draw - string cannot be null." );
//
//	// Store the starting x location
//	int nColStart = nX;
//	float X = (float)nX;
//	float Y = (float)nY;
//
//	// Iterate through the string until reaching the null terminator.
//	for( int i = 0; strOut[ i ]; i++ )
//	{
//		// Get the character from the array
//		TCHAR ch = strOut[ i ];
//
//		// Check for white space
//		if( ch == _T(' ') )
//		{
//			//X += (float)m_nCharWidth * fScale;
//			X += LookupTable[0][0] * fScale;
//			continue;
//		}
//		else if( ch == _T('\n') )
//		{
//			Y += (float)m_nCharHeight * fScale;
//			X = (float)nColStart;
//			continue;
//		}
//		else if( ch == _T('\t') )
//		{
//			// Calculate the number of characters printed
//			float curr = (X - nColStart) / (LookupTable[0][0] * fScale);
//
//			// Calculate the spaces to add
//			int spaces = 4 - ((int)curr % 4);
//
//			// Move that many spaces
//			X += (float)spaces * (float)LookupTable[0][0] * fScale;
//			continue;
//		}
//		else if( ch == _T('\0') )
//		{
//			return;
//		}
//
//		// Convert to uppercase?
//		if( m_bOnlyUppercase == true )
//			ch = TCTOUPPER( ch );
//
//		// Calculate the cell ID based on the ASCII value
//		int id = ch - m_cFirstChar;
//		RECT rCell = CellAlgorithm( id );
//		int newSize = LookupTable[id / m_nNumCols][id % m_nNumCols];
//
//		// Draw the character
//		CSGD_TextureManager::GetInstance()->Draw(
//			m_nImageID, (int)X, (int)Y,
//			fScale, fScale, &rCell,
//			0.0f, 0.0f, 0.0f,
//			dwColor );
//
//		// Move to the next position
//		X += newSize * fScale;
//	}
//}

//RECT MenuFont::CellAlgorithm( int id ) const				
//{
//	//RECT rCell = { };
//	//
//	//rCell.left		= (id % m_nNumCols) * m_nCharWidth;
//	//rCell.top		= (id / m_nNumCols) * m_nCharHeight;
//	//
//	//rCell.right		= rCell.left + LookupTable[id / m_nNumCols][id % m_nNumCols];	//LookupTable[id % m_nNumCols][id / m_nNumCols];
//	//rCell.bottom	= rCell.top	 + m_nCharHeight;
//	//
//	//return rCell;
//
//	return Font::CellAlgorithm(id);
//}

void TitleFont::CreateLookup()
{
	int arrLookup[9][11];

	if(language == CGame::GetInstance()->English)
	{
		// Hard-coded lookup table
		arrLookup[0][0]		= 8;
		arrLookup[0][1]		= 20;
		arrLookup[0][2]		= 22;
		arrLookup[0][3]		= 31;
		arrLookup[0][4]		= 27;
		arrLookup[0][5]		= 35;
		arrLookup[0][6]		= 33;
		arrLookup[0][7]		= 15;
		arrLookup[0][8]		= 16;
		arrLookup[0][9]		= 16;
		arrLookup[0][10]	= 24;
						  
		arrLookup[1][0]		= 31;
		arrLookup[1][1]		= 21;
		arrLookup[1][2]		= 26;
		arrLookup[1][3]		= 20;
		arrLookup[1][4]		= 27;
		arrLookup[1][5]		= 27;
		arrLookup[1][6]		= 24;
		arrLookup[1][7]		= 27;
		arrLookup[1][8]		= 27;
		arrLookup[1][9]		= 40;
		arrLookup[1][10]	= 27;
							
		arrLookup[2][0]		= 28;
		arrLookup[2][1]		= 32;
		arrLookup[2][2]		= 27;
		arrLookup[2][3]		= 27;
		arrLookup[2][4]		= 22;
		arrLookup[2][5]		= 22;
		arrLookup[2][6]		= 18;
		arrLookup[2][7]		= 25;
		arrLookup[2][8]		= 20;
		arrLookup[2][9]		= 28;
		arrLookup[2][10]	= 32;
							
		arrLookup[3][0]		= 32;
		arrLookup[3][1]		= 29;
		arrLookup[3][2]		= 28;
		arrLookup[3][3]		= 30;
		arrLookup[3][4]		= 29;
		arrLookup[3][5]		= 32;
		arrLookup[3][6]		= 27;
		arrLookup[3][7]		= 32;
		arrLookup[3][8]		= 20;
		arrLookup[3][9]		= 26;
		arrLookup[3][10]	= 32;
							
		arrLookup[4][0]		= 28;
		arrLookup[4][1]		= 44;
		arrLookup[4][2]		= 34;
		arrLookup[4][3]		= 27;
		arrLookup[4][4]		= 30;
		arrLookup[4][5]		= 27;
		arrLookup[4][6]		= 32;
		arrLookup[4][7]		= 27;
		arrLookup[4][8]		= 35;
		arrLookup[4][9]		= 35;
		arrLookup[4][10]	= 32;
							
		arrLookup[5][0]		= 44;
		arrLookup[5][1]		= 34;
		arrLookup[5][2]		= 34;
		arrLookup[5][3]		= 34;
		arrLookup[5][4]		= 16;
		arrLookup[5][5]		= 28;
		arrLookup[5][6]		= 16;
		arrLookup[5][7]		= 25;
		arrLookup[5][8]		= 44;
		arrLookup[5][9]		= 22;
		arrLookup[5][10]	= 33;
							
		arrLookup[6][0]		= 27;
		arrLookup[6][1]		= 28;
		arrLookup[6][2]		= 28;
		arrLookup[6][3]		= 28;
		arrLookup[6][4]		= 28;
		arrLookup[6][5]		= 27;
		arrLookup[6][6]		= 32;
		arrLookup[6][7]		= 22;
		arrLookup[6][8]		= 24;
		arrLookup[6][9]		= 34;
		arrLookup[6][10]	= 22;
							
		arrLookup[7][0]		= 44;
		arrLookup[7][1]		= 32;
		arrLookup[7][2]		= 27;
		arrLookup[7][3]		= 27;
		arrLookup[7][4]		= 27;
		arrLookup[7][5]		= 28;
		arrLookup[7][6]		= 28;
		arrLookup[7][7]		= 28;
		arrLookup[7][8]		= 36;
		arrLookup[7][9]		= 32;
		arrLookup[7][10]	= 44;
							
		arrLookup[8][0]		= 34;
		arrLookup[8][1]		= 34;
		arrLookup[8][2]		= 34;
		arrLookup[8][3]		= 22;
		arrLookup[8][4]		= 15;
		arrLookup[8][5]		= 22;
		arrLookup[8][6]		= 0;
		arrLookup[8][7]		= 0;
		arrLookup[8][8]		= 0;
		arrLookup[8][9]		= 0;
		arrLookup[8][10]	= 0;
							  
		/*arrLookup[9][0]		= 17;
		arrLookup[9][1]		= 11;
		arrLookup[9][2]		= 12;
		arrLookup[9][3]		= 17;
		arrLookup[9][4]		= 11;
		arrLookup[9][5]		= 24;
		arrLookup[9][6]		= 17;
		arrLookup[9][7]		= 15;
		arrLookup[9][8]		= 19;
		arrLookup[9][9]		= 18;
		arrLookup[9][10]	= 8;

		arrLookup[10][0]	=	15;
		arrLookup[10][1]	=	15;
		arrLookup[10][2]	=	14;
		arrLookup[10][3]	=	15;
		arrLookup[10][4]	=	13;
		arrLookup[10][5]	=	18;
		arrLookup[10][6]	=	17;
		arrLookup[10][7]	=	24;
								 
		arrLookup[11][0]	=	 17;
		arrLookup[11][1]	=	 17;
		arrLookup[11][2]	=	 17;
		arrLookup[11][3]	=	 11;
		arrLookup[11][4]	=	 8;
		arrLookup[11][5]	=	 11;
		arrLookup[11][6]	=	 35;
		arrLookup[11][7]	=	 12;*/

		this->LookupTable = new int*[9];

		for(int i = 0; i < m_nNumRows; i++)
		{
			LookupTable[i] = nullptr;
			LookupTable[i] = new int[11];
		}

		for(int x = 0; x < m_nNumRows; x++)
			for(int y = 0; y < m_nNumCols; y++)
				LookupTable[x][y] = arrLookup[x][y];
	}
}